package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryHistoryBo;
import org.eiahe.hr.salary.domain.vo.SalaryHistoryVo;
import org.eiahe.hr.salary.service.ISalaryHistoryService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 薪资历史Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/history")
public class SalaryHistoryController extends BaseController {

    private final ISalaryHistoryService iSalaryHistoryService;

    /**
     * 查询薪资历史列表
     */
    @SaCheckPermission("salary:history:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryHistoryVo> list(SalaryHistoryBo bo, PageQuery pageQuery) {
        return iSalaryHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出薪资历史列表
     */
    @SaCheckPermission("salary:history:export")
    @Log(title = "薪资历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryHistoryBo bo, HttpServletResponse response) {
        List<SalaryHistoryVo> list = iSalaryHistoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "薪资历史数据", SalaryHistoryVo.class, response);
    }

    /**
     * 获取薪资历史详细信息
     *
     * @param historyId 主键
     */
    @SaCheckPermission("salary:history:query")
    @GetMapping("/{historyId}")
    public R<SalaryHistoryVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("historyId") Long historyId) {
        return R.ok(iSalaryHistoryService.queryById(historyId));
    }

    /**
     * 新增薪资历史
     */
    @SaCheckPermission("salary:history:add")
    @Log(title = "薪资历史", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryHistoryBo bo) {
        return toAjax(iSalaryHistoryService.insertByBo(bo));
    }

    /**
     * 修改薪资历史
     */
    @SaCheckPermission("salary:history:edit")
    @Log(title = "薪资历史", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryHistoryBo bo) {
        return toAjax(iSalaryHistoryService.updateByBo(bo));
    }

    /**
     * 删除薪资历史
     *
     * @param historyIds 主键串
     */
    @SaCheckPermission("salary:history:remove")
    @Log(title = "薪资历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{historyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] historyIds) {
        return toAjax(iSalaryHistoryService.deleteWithValidByIds(Arrays.asList(historyIds)));
    }
}